﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using POS.UserControl.Dialog;
using Model;
using DevExpress.Utils;
using POS.Framework;
using Model.Bill;
using BLL.Enums;
using BLL;
using DevExpress.Data.Filtering;
using DevExpress.Xpo;

namespace POS.ModForm.Bill
{
    public partial class frmStoreHouseInForm : DevExpress.XtraEditors.XtraForm
    {
        TB_StoreHouseIn fTB_StoreHouseIn = null;
        MyGridChedkMarksSelectionHelper ChedkSelect { get; set; }
        public frmStoreHouseInForm()
        {
            InitializeComponent();
        }
        private void frmStoreHouseInForm_Load(object sender, EventArgs e)
        {
            using (WaitDialogForm wdf = new WaitDialogForm(MsgBox.WaitLoading))
            {
                //this.gridView.CustomDrawRowIndicator += new DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventHandler(SpecialGridView.DrawRowIndicator);
                //this.ChedkSelect = new MyGridChedkMarksSelectionHelper(this.gridView);
                SetButtonClickEvent();
            }
        }
        void SetButtonClickEvent()
        {
            this.formButtonUC.IsHide(new List<string> { "btnSearch", "btnNew", "btnAntiAudit", "btnQuote", "btnPrint", "btnImport", "btnExport" });
            this.formButtonUC.Save.Click += new EventHandler(Save_Click);
            //this.formButtonUC.Examine.Click += new EventHandler(Examine_Click);
        }
        void Save_Click(object sender, EventArgs e)
        {
            if (fTB_StoreHouseIn != null && fTB_StoreHouseIn.StatusID ==(int)StoreHouseInStatus.Complete)
            {
                MsgBox.ShowWarning("入库单已审核！");
                return;
            }
            if (string.IsNullOrEmpty(fProviderID))
            {
                MsgBox.ShowWarning("请选择供应商,入库单需要供货商！");
                //this.btnProvider.Select();
                return;
            }
            //if (storeHouseUC.StoreHouseCode.Length == 0)
            //{
            //    MsgBox.ShowWarning("请选择入库仓库,入库单需要入库仓库！");
            //    storeHouseUC.Select();
            //    return;
            //}
            using (WaitDialogForm wdf = new WaitDialogForm(MsgBox.WaitLoading))
            {
                Session s = DatabaseSchema.Session;
                if (fTB_StoreHouseIn == null)
                {
                    fTB_StoreHouseIn = new TB_StoreHouseIn(s);
                    fTB_StoreHouseIn.BillCode = PlanIDBLL.GetPlanID(s, "RK");
                    fTB_StoreHouseIn.StatusID = (int)StoreHouseInStatus.Editor;
                }

                fTB_StoreHouseIn.ProviderID = s.GetObjectByKey<B_Provider>(fProviderID);
                //fTB_StoreHouseIn.StoreHouseInID = s.FindObject<B_Warehouse>(CriteriaOperator.Parse("StoreHouseCode",""));
                fTB_StoreHouseIn.Save();
                this._BillHead.Text = string.Format("入库单号：{0}  状态：{1}", fTB_StoreHouseIn.BillCode, EnumManage.GetBillStatus(typeof(StoreHouseInStatus), fTB_StoreHouseIn.StatusID));
                wdf.Close();
            }
            MsgBox.ShowTips("入库单基本信息保存成功！");
        }
        string fProviderID = string.Empty;
        private void btnProvider_ButtonPressed(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            using (frmPopProvider fpp = new frmPopProvider())
            {
                if (fpp.ShowDialog() == DialogResult.OK)
                {
                    B_Provider fProvider = fpp.GetSelectRow();
                    fProviderID = fProvider.ProviderID;
                    //this.btnProvider.Text = fProvider.ProviderName;
                }
            }
        }
    }
}